﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		EmailRepository.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 //   File:            EmailRepository.cs
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Collections.Generic;
using Dexter.Core.Data.Repository;
using Dexter.Domain.Model;
using Dexter.Logger.Contracts;
using Dexter.Repository.NHibernate.Queries;
using Dexter.Repository.NHibernate.SessionFactory;
using Topics.Radical.Validation;

namespace Dexter.Repository.NHibernate.Repositories {
	/// <summary>
	/// The implementation of <see cref="IEmailRepository"/>
	/// </summary>
	/// <example>The following is an example of initializing 
	///          an <c>EmailRepository</c> type:
	///   <code>
	///       // Create the type.
	///       EmailRepository ds = new EmailRepository;
	/// 
	///       if ( null == ds )
	///           return;
	/// 
	///       return ds.DoSomething( 5 );
	///   </code>
	/// </example>
	public class EmailRepository : RepositoryBase <EmailMessage> , IEmailRepository {
		/// <summary>
		/// Initializes a new instance of the <see cref="EmailRepository"/> class.
		/// </summary>
		/// <param name="sessionFactory">The session factory.</param>
		/// <param name="logger">The logger.</param>
		public EmailRepository ( ISessionFactory sessionFactory , ILogger logger ) : base ( sessionFactory , logger ) {}

		#region Implementation of IEmailRepository

		/// <summary>
		/// Queues the specified email into dispatcher list.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <exception cref="ArgumentNullException"> If <paramref name="item"/> is <c>null</c>.</exception>
		public virtual void Queue ( EmailMessage item ) {
			Ensure.That ( item )
				.Named ( "item" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsNotNull ( )
				.WithMessage ( "Invalid email message." );

			item.ValidateForQueue ( );

			Session.SaveOrUpdate ( item );
		}

		/// <summary>
		/// Peeks a set of <c>EmailMessage</c> from the queue.
		/// </summary>
		/// <param name="size">The size of return's array.</param>
		/// <returns>
		/// An array of <c>EmailMessage</c>.
		/// The return object could be null if there isn't items into it.
		/// </returns>
		/// <exception cref="ArgumentNullException"> If <paramref name="size"/> is lesser then 1.</exception>
		public virtual IEnumerable<EmailMessage> Peek ( int size ) {
			Ensure.That ( size )
				.Named ( "size" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsGreaterThen ( 0 , Or.Equal )
				.WithMessage ( "Invalid size." );

			return Session.GetNamedQuery ( KnownQueries.EmailRepository.Peek )
				.SetMaxResults ( size )
				.List <EmailMessage> ( );
		}

		/// <summary>
		/// The lifecycle of the item is completed, so this method removes it from the repository.
		/// </summary>
		/// <param name="item">The item to remove.</param>
		/// <exception cref="ArgumentNullException"> If <paramref name="item"/> is <c>null</c>.</exception>
		public virtual void Dequeue ( EmailMessage item ) {
			Ensure.That ( item )
				.Named ( "item" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsNotNull ( )
				.WithMessage ( "Invalid email message." );

			item.ValidateForDequeue ( );

			Session.Delete ( item );
		}

		#endregion
	}
}
